package com.wwg.market.ui.code.client.widgets;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.mobileares.midp.mui.client.table.UFishTable;
import com.mobileares.midp.widgets.client.anchor.TextAnchor;
import com.mobileares.midp.widgets.client.menu.PopMenu;
import com.mobileares.midp.widgets.client.pop.IMessageConfirm;
import com.mobileares.midp.widgets.client.pop.Message;
import com.mobileares.midp.widgets.client.utils.IWidgetProvider;
import com.nexustar.gwt.widgets.client.asyn.LoadingAsyncCallback;
import com.nexustar.gwt.widgets.client.ui.table.*;
import com.wwg.market.ui.code.client.CodeFormFactory;
import com.wwg.market.ui.code.client.CodePageItem;
import com.wwg.market.ui.dashboard.client.Utils;
import com.wwg.market.ui.service.dto.GeneralCodeDto;
import com.wwg.market.ui.service.interfaces.CodeService;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Created by IntelliJ IDEA.
 *
 * @author gaoyang
 * @version $Id: $
 * @date 11-12-21 下午5:28
 */
public class ExListWidget extends Composite {
    private int width = 300;
    private int height = 300;
    //    private HorizontalPanel container;
    private UFishTable<GeneralCodeDto> listWidget;
    private CodeListWidget  childWidget;

    public ExListWidget() {
        listWidget = getUFishListWidget();
        initWidget(listWidget);
    }

    public void setSelectionHandler(final SelectionHandler<TableItem> handler) {
        listWidget.getBMUI().addSelectionHandler(new SelectionHandler<TableItem>() {
            public void onSelection(SelectionEvent<TableItem> tableItemSelectionEvent) {
                handler.onSelection(tableItemSelectionEvent);
            }
        });
    }

    private UFishTable<GeneralCodeDto> getUFishListWidget() {
        ViewTable table = new ViewTable(getCW());
        UFishTable<GeneralCodeDto> list = new UFishTable(table);
        list.getBMUI().setWidth(width + "px");
        list.getBMUI().setHeight(height + "px");
        return list;
    }

    private ColumnWaiter getCW() {
        ColumnExt c1 = new ColumnExt("类别名称", 100, new IColumnRender() {
            public void renderColumn(CellItem cellItem) {
                GeneralCodeDto dto = (GeneralCodeDto) cellItem.getUserObject();
                cellItem.setText(dto.getName());
            }
        });
        ColumnExt c2 = new ColumnExt("标示", 100, new IColumnRender() {
            public void renderColumn(CellItem cellItem) {
                GeneralCodeDto dto = (GeneralCodeDto) cellItem.getUserObject();
                cellItem.setText(dto.getCode());
            }
        });
        ColumnExt c3 = new ColumnExt("操作", 150, new IColumnRender() {
            public void renderColumn(final CellItem cellItem) {
                HorizontalPanel container = new HorizontalPanel();
                final GeneralCodeDto dto = (GeneralCodeDto) cellItem.getUserObject();
                TextAnchor oper = new TextAnchor("修改");
                oper.addClickHandler(new ClickHandler() {
                    public void onClick(ClickEvent clickEvent) {
                        CodeFormFactory.getEditCodeForm().setModel(listWidget.getFishModel());
                        CodeFormFactory.getEditCodeForm().show(dto);
                    }
                });
                TextAnchor add = new TextAnchor("添加代码");
                add.addClickHandler(new ClickHandler() {
                    public void onClick(ClickEvent clickEvent) {
//                        if (createChildCommand != null)
//                            createChildCommand.execute();
                        CodeFormFactory.getEditCodeForm().setModel(childWidget.getListWidget().getFishModel());
                        GeneralCodeDto child = new GeneralCodeDto();
                        child.setParent(dto.getId());
                        CodeFormFactory.getEditCodeForm().show(child);

                    }
                });
                TextAnchor delete = new TextAnchor("删除");
                delete.addClickHandler(new ClickHandler() {
                    public void onClick(ClickEvent clickEvent) {
                        Message.showConfirm("温馨提示", "是否要删除当前类别?", new IMessageConfirm() {
                            public void fire(boolean confirm) {
                                if (confirm) {
                                    Utils.invoke(CodeService.class.getName(), "delete", new Object[]{dto}, new LoadingAsyncCallback() {
                                        @Override
                                        protected void success(Object o) {
                                            listWidget.getFishModel().deleteItem(dto);
                                        }
                                    });
                                }
                            }
                        });
                    }
                });
//
                container.add(add);
                container.add(oper);
                container.add(delete);
                cellItem.setWidget(container);
            }
        });
        return new ColumnWaiter(new ColumnExt[]{c1, c2, c3});
    }

    public void render() {
        listWidget.getFishModel().render(listWidget.getFishWidget());
    }

    public UFishTable<GeneralCodeDto> getListWidget() {
        return listWidget;
    }

    public void setChildWidget(CodeListWidget childWidget) {
        this.childWidget = childWidget;
    }
}
